import * as events from 'events';
import { LoginProto } from './LoginProto';
import http = require('http');
import queryString  = require("querystring");
import url = require('url');

export class Login extends events.EventEmitter
{

    constructor()
    {
        super();
    }

    /**服务器不关心method类型，统一解析url */
    public handle(req:http.IncomingMessage,res:http.ServerResponse):void
    {
        let send_str = '';//需要发送的内容
        let obj = url.parse(req.url);
        let query = obj.query;
        console.log(query);
        let params = queryString.parse(query);
        console.log(params);
        let base64 = params.credential;
        let acc_pas = new Buffer(base64, 'base64').toString();
        console.log(acc_pas);
        let arr = acc_pas.split(':');
        let account = arr[0];
        let password = arr[1];
        //去数据库验证
        let proto = new LoginProto();
        proto.code = 0;
        proto.body.result = 1;
        let json_str = JSON.stringify(proto);
        console.log(json_str);
        res.end(json_str);
    }
}